游戏编辑弹窗
<template>
  <el-dialog
    :title="isUpdate ? '修改游戏任务' : '添加游戏任务'"
    :visible="visible"
    width="800px"
    :destroy-on-close="true"
    :lock-scroll="false"
    :before-close="confirmClose"
    @update:visible="updateVisible"
    :close-on-click-modal="false"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="85px">
      <el-form-item label="日期:" prop="time_range">
        <el-date-picker
          style="width: 100%"
          v-model="form.time_range"
          type="datetimerange"
          value-format="yyyy-MM-dd HH:mm:ss"
          label-format="yyyy-MM-dd HH:mm:ss"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="游戏:" prop="game_id">
        <el-select
          clearable
          v-model="form.game_id"
          placeholder="请选择游戏"
          class="ele-fluid"
          @change="getServerNames"
        >
          <el-option
            v-for="item in gameNames"
            :key="item.id"
            :label="item.game_name"
            :value=item.id
          />
          <el-pagination
            small
            layout="prev, pager, next"
            @current-change="changeGameNumber"
            :hide-on-single-page="true"
            :page-size="pageSize"
            :current-page="currentPage"
            :page-count="pageCount"
            :total="pageTotal"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="区服:" prop="server_id">
        <el-select
          clearable
          class="ele-block"
          v-model="form.server_id"
          placeholder="请选择区服"
        >
          <el-option
            v-for="(v, i) in serverNames"
            :key="i"
            :value="v.server_id"
            :label="v.name"
          />
          <el-pagination
            small
            layout="prev, pager, next"
            @current-change="changeNumber"
            :hide-on-single-page="true"
            :page-size="pageSize"
            :current-page="currentPage"
            :page-count="pageCount"
            :total="pageTotal"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="任务名称:">
        <el-select
          clearable
          v-model="form.task_type"
          placeholder="请选择任务名称"
          class="ele-fluid"
        >
          <el-option label="请选择" value="" />
          <el-option label="元气任务" :value="1" />
          <el-option label="武功任务" :value="2" />
          <el-option label="充值任务" :value="3" />
          <el-option label="年龄任务" :value="4" />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="任务名称:" prop="task_name">
        <el-input v-model="form.task_name" placeholder="请输入任务名称" clearable />
      </el-form-item> -->
      <el-form-item label="任务条件:" prop="task_condition">
        <el-input-number
          v-model="form.task_condition"
          :min="0"
          :max="1000000"
          label="请输入积分奖励"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="排序:" prop="sort">
        <el-input-number
          v-model="form.sort"
          :min="1"
          :max="10000"
          label="排序"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="启用状态:" prop="status">
        <el-radio-group v-model="form.status">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="2">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="任务奖励">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="small"
          @click="addPropNewItem"
          >继续添加</el-button
        >
        <el-table :data="propAttrTable" border style="width: 100%">
          <el-table-column label="ICON">
            <template slot-scope="scope">
              <uploadImage :limit="1" v-model="scope.row.prop_icon"></uploadImage>
            </template>
          </el-table-column>
          <el-table-column label="道具ID">
            <template slot-scope="scope">
              <el-input v-model="scope.row.prop_id"></el-input>
            </template>
          </el-table-column>
          <el-table-column label="道具名">
            <template slot-scope="scope">
              <el-input v-model="scope.row.prop_name"></el-input>
            </template>
          </el-table-column>
          <el-table-column label="道具数">
            <template slot-scope="scope">
              <el-input v-model="scope.row.prop_num"></el-input>
            </template>
          </el-table-column>
          <el-table-column width="100px" label="操作">
            <template slot-scope="scope">
              <i
                class="el-icon-delete"
                @click="delPropAttr(scope.$index)"
                style="cursor: pointer"
              ></i>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item label="备注:" prop="info">
        <el-input v-model="form.info" placeholder="请输入备注" clearable />
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="confirmClose()">取消</el-button>
      <el-button type="primary" @click="save" :loading="loading"
        >保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import axios from "axios";
import uploadImage from "@/components/taskIconUploadImage";
export default {
  name: "GameTaskEdit",
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    TaskData: Object,
    //游戏列表
    // gameNames: Array
  },
  components: { uploadImage },
  data() {
    return {
      // 表单数据
      form: Object.assign({ status: 1 }, this.TaskData),
      where: {},
      // 表单验证规则
      rules: {
        time_range: [
          { required: true, message: "请选择开始和结束时间", trigger: "blur" }
        ],
        game_id: [{ required: true, message: "请选择游戏", trigger: "blur" }],
        server_id: [{ required: true, message: "请选择区服", trigger: "blur" }],
        task_name: [
          { required: true, message: "请输入任务名称", trigger: "blur" }
        ],
        task_type: [
          { required: true, message: "请输入任务类型", trigger: "blur" }
        ],
        task_condition: [
          { required: true, message: "请输入任务条件", trigger: "blur" }
        ],
        task_award: [
          { required: true, message: "请输入任务奖励", trigger: "blur" }
        ],
        score: [{ required: true, message: "请输入积分奖励", trigger: "blur" }],
        sort: [{ required: true, message: "请输入排序", trigger: "blur" }],
        status: [{ required: true, message: "请选择状态", trigger: "blur" }]
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      //区服列表
      serverNames: [],
      //分页功能
      pageSize: 100, //每页显示条数  6条刚好
      currentPage: 1, //当前页数
      pageCount: 6, //按钮数，超过时会折叠
      pageTotal: 0, //总条数

      //道具列表
      propAttrTable: [],
      gameNames:[]
    };
  },
  watch: {
    visible(val) {
      this.changeGameNumber()
      this.propAttrTable = [];
      if (val) {
        if (this.TaskData) {
          this.form = Object.assign(
            {},
            {
              ...this.TaskData,
              time_range: [this.TaskData.start_time, this.TaskData.end_time]
            }
          );
          
          if (this.form.task_award) {
            this.propAttrTable = JSON.parse(this.TaskData.task_award);
          }

          this.changeNumber()

          this.isUpdate = true;
        } else {
          this.form = {};
          this.isUpdate = false;
        }
      }
    }
  },
  methods: {
    save() {
      this.form.task_award = this.propAttrTable;
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.loading = true;
          this.$http
            .post("/boxGametaskConfig/edit", this.form)
            .then(res => {
              this.loading = false;
              if (res.data.code === 0) {
                this.$message.success(res.data.msg);
                if (!this.isUpdate) {
                  this.form = {};
                }
                this.updateVisible(false);
                this.$emit("done");
                this.form = {};
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch(e => {
              this.loading = false;
              this.$message.error(e.message);
            });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit("update:visible", value);
    },

    confirmClose() {
      this.$confirm("确定要返回吗？", "温馨提示")
        .then(() => {
          this.updateVisible(false);
          // this.initForm();
          this.form = {};
          this.serverNames = []
        })
        .catch(() => {});
    },
    // 删除规格
    delPropAttr(index) {
      this.propAttrTable.splice(index, 1);
    },

    // 添加信息规格
    addPropNewItem() {
      this.propAttrTable.push({ prop_id: "", prop_name: "", prop_num: "" });
    },

    getServerNames(game_id) {
      this.form.game_id = game_id;
      axios
        .post("/BoxGameOpen/index", {
          page: this.currentPage,
          limit: this.pageSize,
          game_id: game_id
        })
        .then(res => {
          if (res.data.code != 0) {
            console.log("获取数据失败！");
            return;
          }
          this.serverNames = res.data.data;
          this.pageTotal = res.data.count;
        });
    },
    changeNumber(val) {
      axios
        .post("/BoxGameOpen/index", {
          page: val,
          limit: this.pageSize,
          game_id: this.form.game_id
        })
        .then(res => {
          this.serverNames = res.data.data;
        });
    },
    changeGameNumber(val){
      axios
        .post("/boxGame/index", {
          page: val ? val :this.currentPage,
          limit: this.pageSize,
        })
        .then(res => {
          this.gameNames = res.data.data;
          this.pageTotal = res.data.count
        });
    },

  }
};
</script>

<style scoped></style>
